package blue.qingyu.whimurmur.lookBack.directive;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import blue.qingyu.whimurmur.lookBack.model.LookBack;
import blue.qingyu.whimurmur.lookBack.service.LookBackServiceProvider;
import io.jboot.db.model.Column;
import io.jboot.db.model.Columns;
import io.jboot.utils.DateUtil;
import io.jboot.web.controller.JbootControllerContext;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jboot.web.directive.base.PaginateDirectiveBase;
import io.jpress.commons.directive.DirectveKit;

import java.sql.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**
 * @author zhangxr
 * @Title:
 * @Package
 * @Description:
 * @date 2020-7-19 22:47
 */
@JFinalDirective("tracePage")
public class LookBackPageDirective extends JbootDirectiveBase {

    @Inject
    private LookBackServiceProvider traceService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
    	Controller controller = JbootControllerContext.get();
    	String page = controller.getRequest().getParameter("page");
    	String title = controller.getRequest().getParameter("title");
    	
    	int pageInt = 1;
    	if(!StringUtils.isEmpty(page)) {
    		pageInt = Integer.parseInt(page);new Date(1l).toString();
    	}
        int pageSize = getPara("pageSize", scope, 15);
        String orderBy = getPara("orderBy", scope, "`date` desc");

        Columns columns = Columns.create();
        if(StringUtils.isNotEmpty(title)) {
        	columns.likeAppendPercent("title", title);
    	}
        // 自定义查询条件

        Page<LookBack> tracePage = traceService.paginateByColumns(pageInt, pageSize, columns, orderBy);

        scope.setGlobal("tracePage", tracePage);
        renderBody(env, scope, writer);
    }

    @Override
    public boolean hasEnd(){
        return true;
    }

    @JFinalDirective("tracePaginate")
    public static class TracePaginateDirective extends PaginateDirectiveBase {

        @Override
        protected String getUrl(int pageNumber, Env env, Scope scope, Writer writer) {
            HttpServletRequest request = JbootControllerContext.get().getRequest();
            String url = request.getRequestURI();
            String contextPath = JFinal.me().getContextPath();

            boolean firstGotoIndex = getPara("firstGotoIndex", scope, false);

            if (pageNumber == 1 && firstGotoIndex) {
                return contextPath + "/";
            }

            return DirectveKit.replacePageNumber(url, pageNumber);
        }

        @Override
        protected Page<?> getPage(Env env, Scope scope, Writer writer) {
            return (Page<?>) scope.get("tracePage");
        }
    }
}
